package com.example.myapplication.Garbagesorting;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.CarouselmapSearchBean;
import com.example.myapplication.ShuJu.SearchpicturegridBean;
import com.example.myapplication.apter.CarouselmapSearchApter;
import com.example.myapplication.apter.SearchpicturegridApter;

public class SearchActivity extends AppCompatActivity {

    private Button but_search;
    private SearchView se01_search;
    private ViewPager se_vp;
    private EditText ed_search;
    private RecyclerView se_re;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initview();
        //返回
        but_bloak();
        //轮播图
        carouselmap();
        //图片宫格
        picturegrid();
    }

    private void picturegrid() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/garbage-example/list",
                OKHttpTool.GET,
                SearchpicturegridBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        SearchpicturegridBean searchpicturegridBean = (SearchpicturegridBean)obj;
                        if(searchpicturegridBean.getCode()==200){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    SearchpicturegridApter searchpicturegridApter = new SearchpicturegridApter(SearchActivity.this,searchpicturegridBean.getRows(),16);
                                    GridLayoutManager gridLayoutManager = new GridLayoutManager(SearchActivity.this,4);
                                    se_re.setAdapter(searchpicturegridApter);
                                    se_re.setLayoutManager(gridLayoutManager);

                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void but_bloak() {
        but_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchActivity.this.finish();
            }
        });
    }

    private void carouselmap() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/poster/list", OKHttpTool.GET, CarouselmapSearchBean.class, new OKHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                CarouselmapSearchBean carouselmapSearchBean = (CarouselmapSearchBean)obj;
                if(carouselmapSearchBean.getCode()==200){
                    CarouselmapSearchApter carouselmapSearchApter = new CarouselmapSearchApter(SearchActivity.this,carouselmapSearchBean.getData());
                    se_vp.setAdapter(carouselmapSearchApter);
                    mhandler.sendEmptyMessageDelayed(0,3*1000);
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void initview() {
        but_search = findViewById(R.id.but_search);
        se01_search = findViewById(R.id.se01_Search);
        se_vp = findViewById(R.id.se_vp);
        ed_search = findViewById(R.id.ed_Search);
        se_re = findViewById(R.id.se_re);
    }

    private Handler mhandler =  new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                int currentItem = se_vp.getCurrentItem();
                currentItem = (currentItem+1) % 3 ;
                se_vp.setCurrentItem(currentItem);
                mhandler.sendEmptyMessageDelayed(0,3*1000);
            }
        }
    };
}